<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;

class CloudaccountwithdrawController
{

    private $app_dealer_id = '';
    private $app_broker_id = '';
    private $app_key = '';
    private $app_des3_key = '';
    private $app_private_key = '';
    private $yzh_public_key = '';

    public function __construct(){
        $config_arr = require __DIR__."/../../../adminconfig/yzhconfig.php";
        $this->app_dealer_id = $config_arr['app_dealer_id'];
        $this->app_broker_id = $config_arr['app_broker_id'];
        $this->app_key = $config_arr['app_key'];
        $this->app_des3_key = $config_arr['app_des3_key'];
        $this->app_private_key = $config_arr['app_private_key'];
        $this->yzh_public_key = $config_arr['yzh_public_key'];
    }

    /**
     * 云账户提现结果回调
     * User:@hang
     * Date:2023-09-18 21:13
     */
    public function yzhcallback(){
        $data = input('data');
        $mess = input('mess');
        $timestamp = input('timestamp');
        $sign = input('sign');
        if(empty($data) || empty($mess) || empty($timestamp) || empty($sign)){
            fwrite(fopen('./uploads/adminyunzhanghutxhd.txt', 'a+'), "云账户回调信息不全".date("Y-m-d H:i:s")."\r\n");
        }
        // 配置基础信息
        $config = \Yzh\Config::newFromArray(array(
            "app_dealer_id" => $this->app_dealer_id,
            "app_broker_id" => $this->app_broker_id,
            "app_key" => $this->app_key,
            "app_des3_key" => $this->app_des3_key,
            "app_private_key" => $this->app_private_key,
            "yzh_public_key" => $this->yzh_public_key,
            "sign_type" => "rsa"
        ));
//        fwrite(fopen('./uploads/adminyunzhanghutxhd_log.txt', 'a+'), "信息:".json_encode($data).'@@@@'.json_encode($mess).'@@@@'.json_encode($timestamp).'@@@@'.json_encode($sign).'@@@@'.date("Y-m-d H:i:s")."\r\n");
        $notifyClient = new \Yzh\NotifyClient($config);
        // 异步回调参数
        $notifyReq = new \Yzh\Model\Notify\NotifyRequest ($data, $mess, $timestamp, $sign) ;
        // 发起验签解密
        try {
            $result = $notifyClient->verifyAndDecrypt($notifyReq);
            if ($result) {
                // 验签通过、解密成功
                fwrite(fopen('./uploads/adminyunzhanghutxhd.txt', 'a+'), "json:".$result->getData().date("Y-m-d H:i:s")."\r\n");
                $arr = json_decode($result->getData(),true);
                fwrite(fopen('./uploads/adminyunzhanghutxhd.txt', 'a+'), "data:".$arr['data']['order_id'].'/////'.$arr['data']['status'].'/////'.$arr['data']['withdraw_platform'].'/////'.date("Y-m-d H:i:s")."\r\n\r\n");
            }
        }
        catch(Exception $e){
            // 发生异常
            fwrite(fopen('./uploads/adminyunzhanghutxhd.txt', 'a+'), "发生异常:".$e->getMessage ().date("Y-m-d H:i:s")."\r\n");
        }
        return 'success';
    }





}
